﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClinicalMassageSystem
{
    public partial class SearchScreen : Form
    {
        int patientID = 0;
        string name;
        string patientInfo;
        
        public SearchScreen()
        {
            InitializeComponent();
        }

        CMSDataContext cms = new CMSDataContext();

        private void SearchScreen_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnSearch;
        }

        private void runSearch()
        {
            try
            {
                if (txtPatientID.Text != "")
                    patientID = int.Parse(txtPatientID.Text);
                name = txtName.Text;

                if (!name.Equals(""))
                {
                    patientBindingSource.DataSource =
                        from p in cms.Patients
                        where (p.LastName.Contains(name) ||
                            p.FirstName.Contains(name))
                        orderby p.LastName
                        select p;
                }
                else if (patientID > 0)
                {
                    patientBindingSource.DataSource =
                        from p in cms.Patients
                        where (p.PatientID == patientID)
                        orderby p.LastName
                        select p;
                }
                else
                {
                    MessageBox.Show("Enter a Search Parameter");
                }
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            runSearch();
        }

        private void patientDataGridView_DataSourceChanged(object sender, EventArgs e)
        {
            patientDataGridView.Refresh();
        }

        private void patientDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            PatientOverview po = new PatientOverview();
            patientInfo = patientDataGridView.SelectedCells[0].Value.ToString();
            po.setPatientBindings(int.Parse(patientInfo));
            po.Show();
            this.Close();
        }

        private void patientDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        

        
    }
}
